package controller;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

public class UserServlet extends HttpServlet {
    // 重写方法  alt+ insert


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 获取参数
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        String aihao = req.getParameter("aihao");
        String sex = req.getParameter("sex");


        System.out.println("servlet");
        System.out.println(name);
        System.out.println(password);
        System.out.println(aihao);
        System.out.println(sex);
        // session  同一个客户段，多个servlet共享
        HttpSession session = req.getSession();
        session.setAttribute("name", "lisi");

        // 响应页面，  转发、重定向

//        // 往request存放值
        List<User> list = new ArrayList<User>();
        User user = new User(1, "lisi", "1234");
        list.add(user);
        User user1 = new User(2, "wangwu", "321");
        list.add(user1);


        req.setAttribute("list", list);
        req.setAttribute("name", name);
        req.setAttribute("password", password);
        req.setAttribute("aihao", aihao);
        req.setAttribute("sex", sex);
        // 转发 url地址不会变
        req.getRequestDispatcher("user.jsp").forward(req, resp);

        // 重定向 url地址会变
//        resp.sendRedirect("/user.jsp");
    }
}
